package com.zxx.ssm.controller;

import com.zxx.ssm.dao.*;
import com.zxx.ssm.exception.AbstractServiceRuntimeException;
import com.zxx.ssm.model.biz.*;
import com.zxx.ssm.model.convertor.TeacherStudentClassConvertor;
import com.zxx.ssm.model.enums.ResultCode;
import com.zxx.ssm.model.form.TeacherStudentClassForm;
import com.zxx.ssm.model.result.BizResponse;
import com.zxx.ssm.service.*;
import com.zxx.ssm.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.web.bind.annotation.*;

import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("teaStuClass")
public class TeacherStudentClassController {
    @Autowired
    private TeacherStudentClassService teacherStudentClassService;

    @Autowired
    private GuoziClassService guoziClassService;

    @Autowired
    private TeaStuClaLogService teaStuClaLogService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StuService stuService;

    @RequestMapping(value = "getAll", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse getAll(@RequestParam String currentDate){
        Date curDate = DateUtils.toDate(currentDate);
        Date firstDay = DateUtils.getFirstDayOfMonth(curDate);
        Date lastDay = DateUtils.getLastDayInMonth(curDate);
        List<TeaStuClassBiz> teaStuClassBizs = teacherStudentClassService.getAll(firstDay, lastDay);
        BizResponse response = new BizResponse();
        response.put("teaStuClassBizs", teaStuClassBizs);
        return response;
    }

    @RequestMapping(value = "addCourse", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public BizResponse addCourse(@ModelAttribute TeacherStudentClassForm teacherStudentClassForm) {
        try {
            Guoziclass guoziclass = TeacherStudentClassConvertor.convToGuozi(teacherStudentClassForm);
            guoziClassService.insertAndGetId(guoziclass);
            List<TeacherStudentClass> teacherStudentClasses = TeacherStudentClassConvertor.convToTeaStuCla(teacherStudentClassForm, guoziclass.getId());
            for (int i = 0; i < teacherStudentClasses.size(); i++)
                teacherStudentClassService.insertSelective(teacherStudentClasses.get(i));
            List<TeacherStudentClassLog> teacherStudentClassLogs = TeacherStudentClassConvertor.convToTeaStuClaLog(teacherStudentClassForm, guoziclass);
            for (int i = 0; i < teacherStudentClassLogs.size(); i++)
                teaStuClaLogService.insertSelective(teacherStudentClassLogs.get(i));
            return new BizResponse();
        } catch (Exception e){
            return new BizResponse(ResultCode.TEA_STU_CLASS_ERROR);
        }
    }

    @RequestMapping(value = "editCourse", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public BizResponse updateCourse(@ModelAttribute TeacherStudentClassForm teacherStudentClassForm){
        try {
            //删除课程
            String tscids[] = teacherStudentClassForm.getTscIds().split(",");
            teacherStudentClassService.updateCourse(tscids, teacherStudentClassForm.getClassId());
            guoziClassService.delete(teacherStudentClassForm.getClassId());

            //添加课程
            Guoziclass guoziclass = TeacherStudentClassConvertor.convToGuozi(teacherStudentClassForm);
            guoziClassService.insertAndGetId(guoziclass);
            List<TeacherStudentClass> teacherStudentClasses = TeacherStudentClassConvertor.convToTeaStuCla(teacherStudentClassForm, guoziclass.getId());
            for (int i = 0; i < teacherStudentClasses.size(); i++)
                teacherStudentClassService.insertSelective(teacherStudentClasses.get(i));
            List<TeacherStudentClassLog> teacherStudentClassLogs = TeacherStudentClassConvertor.convToTeaStuClaLog(teacherStudentClassForm, guoziclass);
            for (int i = 0; i < teacherStudentClassLogs.size(); i++)
                teaStuClaLogService.insertSelective(teacherStudentClassLogs.get(i));
            return new BizResponse();
        } catch (Exception e){
            return new BizResponse(ResultCode.TEA_STU_CLASS_ERROR);
        }
    }

    @RequestMapping(value = "deleteCourse", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse deleteCourse(String tscIds, Integer classId){
        //删除课程
        String tscids[] = tscIds.split(",");
        try {
            teacherStudentClassService.updateCourse(tscids, classId);
            guoziClassService.delete(classId);
        } catch (AbstractServiceRuntimeException e) {
            e.printStackTrace();
            return new BizResponse(ResultCode.TEA_STU_CLASS_DELETE_ERROR);
        }
        return new BizResponse();
    }

    @RequestMapping(value = "getTeaClassCount", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse getTeaClassCount(@RequestParam Integer teaId){
        Teacher teacher = teacherService.selectByPrimaryKey(teaId);
        List<TeaClasstypeGroupBiz> teaStuClassBizList = teacherStudentClassService.getTeaClassCount(teaId);
        TeaClassCountBiz teaClassCountBiz = TeacherStudentClassConvertor.convToTeaClassCountBiz(teaStuClassBizList);
        teaClassCountBiz.setTeaId(teaId);
        teaClassCountBiz.setTeaName(teacher.getUsername());
        BizResponse bizResponse = new BizResponse();
        bizResponse.put("teaClassCount", teaClassCountBiz);
        return bizResponse;
    }

    @RequestMapping(value = "getStuClassCount", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse getStuClassCount(@RequestParam Integer stuId){
        Student student = stuService.selectByPrimaryKey(stuId);
        List<StuClasstypeGroupBiz> stuClasstypeGroupBizList = teacherStudentClassService.getStuClassCount(stuId);
        StuClassCountBiz stuClassCountBiz = TeacherStudentClassConvertor.convToStuClassCountBiz(stuClasstypeGroupBizList);
        stuClassCountBiz.setStuId(stuId);
        stuClassCountBiz.setStuName(student.getUsername());
        stuClassCountBiz.setApplyListenCount(student.getApplyListenCount());
        stuClassCountBiz.setApplyOralCount(student.getApplyOralCount());
        stuClassCountBiz.setApplyReadCount(student.getApplyReadCount());
        stuClassCountBiz.setApplyWriteCount(student.getApplyWriteCount());
        BizResponse bizResponse = new BizResponse();
        bizResponse.put("stuClassCount", stuClassCountBiz);
        return bizResponse;
    }

    @RequestMapping(value = "getAllTeacherClass", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse getAllTeacherClass(@RequestParam String currentDate, @RequestParam Integer teaId){
        Date curDate = DateUtils.toDate(currentDate);
        Date firstDay = DateUtils.getFirstDayOfMonth(curDate);
        Date lastDay = DateUtils.getLastDayInMonth(curDate);
        List<TeaStuClassBiz> teaStuClassBizs = teacherStudentClassService.getAllTeacherClass(firstDay, lastDay, teaId);
        BizResponse response = new BizResponse();
        response.put("teaStuClassBizs", teaStuClassBizs);
        return response;
    }

    @RequestMapping(value = "getAllStudentClass", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse getAllStudentClass(@RequestParam String currentDate, @RequestParam Integer stuId){
        Date curDate = DateUtils.toDate(currentDate);
        Date firstDay = DateUtils.getFirstDayOfMonth(curDate);
        Date lastDay = DateUtils.getLastDayInMonth(curDate);
        List<TeaStuClassBiz> teaStuClassBizs = teacherStudentClassService.getAllStudentClass(firstDay, lastDay, stuId);
        BizResponse response = new BizResponse();
        response.put("teaStuClassBizs", teaStuClassBizs);
        return response;
    }

    @RequestMapping(value = "searchStudentClass", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse searchStudentClass(@RequestParam String startDate, @RequestParam Integer stuId){
        Date date = DateUtils.toDate(startDate);
        List<TeaStuClassBiz> teaStuClassBizs = teacherStudentClassService.getAllStudentClass(date, null, stuId);
        BizResponse response = new BizResponse();
        response.put("teaStuClassBizs", teaStuClassBizs);
        return response;
    }

}
